import { getTokenInfo } from "@/utils/storage";


type UserInfo = {
  id: string;
  name: string;
  photo: string;
  art_count: number;
  follow_count: number;
  fans_count: number;
  like_count: number;
}
export type UserToken = {
  token: string;
  refresh_token: string;
}

export type Profile = {
  id: string;
  photo: string;
  name: string;
  mobile: string;
  gender: number;
  birthday: string;
  intro: string
}

//定义初始化函数
const initState = {
  userInfo: {} as UserInfo,
  userToken: getTokenInfo(),
  userProfile: {} as Profile
}
type UserType =
  | { type: 'user/token', payload: UserToken }
  | { type: 'user/setInfo', payload: UserInfo }
  | { type: 'user/setProfile', payload: Profile }
  | { type: 'user/setProfileOne', payload: Partial<Profile> }
export default function UserReducer(state = initState, action: UserType) {
  switch (action.type) {
    case 'user/token':
      return { ...state, userToken: action.payload }
    case 'user/setInfo':
      return { ...state, userInfo: action.payload }
    case 'user/setProfile':
      return { ...state, userProfile: action.payload }
    case 'user/setProfileOne':
      return { ...state, userProfile: { ...state.userProfile, ...action.payload } }
    default:
      return state
  }
}